import {defineStore} from "pinia";
import axios from "axios";
import {nanoid} from 'nanoid'

// export const useLoveTalkStore = defineStore('talkList',{
//     // actions 里面放置一个一个的方法，用于响应组件中的动作
//     actions: {
//         async unshiftLoveTalk() {
//             console.log('被调用了unshiftLoveTalk')
//
//             //发送请求
//             let {data} = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
//             let {content:title} =data
//             // // 封装对象
//             let obj = {id:nanoid(),title}
//             this.talkList.unshift(obj)
//         }
//     },
//
//     // 真正存储数据的地方
//     state() {
//         return {
//             talkList:JSON.parse( localStorage.getItem('talkList') as string ) || []
//         }
//     }
// })

import {reactive} from "vue";

export const useLoveTalkStore = defineStore('talkList', () => {
    const talkList = reactive(
        JSON.parse(localStorage.getItem('talkList') as string) || []
    )


    async function unshiftLoveTalk() {
        console.log('被调用了unshiftLoveTalk')

        //发送请求
        let {data} = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
        let {content: title} = data
        // // 封装对象
        let obj = {id: nanoid(), title}
        talkList.unshift(obj)
    }

    return {talkList, unshiftLoveTalk}
})
